package com.jiukuaidao.config;

import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;

/**
 * @author fengjiangshuai@jiuxian.com
 * 2024/4/13 17:00
 */
public class CustomTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
        Authentication userAuthentication = oAuth2Authentication.getUserAuthentication();
        if (userAuthentication != null) {
            Object principal = oAuth2Authentication.getUserAuthentication().getPrincipal();
            HashMap<String, Object> map = new HashMap<>();
            map.put("userDetails", principal);
            ((DefaultOAuth2AccessToken)oAuth2AccessToken).setAdditionalInformation(map);
        }
        return oAuth2AccessToken;
    }
}
